<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: link 
* Author			: 乔伟
* Create Time		: 2011-10-05 
* Modify Time		: 2011-10-05 
* Modify Profile	:  
******************************************************/
class link extends Admin_Controller 
{
	/******************************************************
	 * Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-05
	* Modify Time		: 2011-10-05
	* Modify Profile	:
	******************************************************/
	public function index()
	{
		$where = $this->Request();
	
		!isset($where['page_size']) && $where['page_size'] = 20;
		$where['order_by'] = "id desc";
		$links = $this->link_manager->get_list($where);
	
		$page_size = 20; $page = isset($_GET['page'])?$_GET['page']:1;
		// p_r($links);exit;
		$total_count = $this->link_manager->query_result->record_count;
		$page_count = intval($total_count/$page_size) + 1;
		$pre_page = ($page - 1) < 1 ? 1 : ($page - 1);
		$next_page = ($page + 1) > $page_count ? $page_count : ($page + 1);
	
		$get_str = "";
		if(!isset($_GET))
		{
			$get_str = "?";
		}
		else
		{
			$tmp = array();
			foreach($_GET as $key=>$value)
			{
				if($key != "link")
				{
					$tmp[] = "$key=$value";
				}
			}
			$get_str = "?".implode('&',$tmp)."&";
		}
	
		$sp_config = array(
				'base_url' => _U('link/index'.$get_str,true),
				'current_page'=>$page,
				'pre_page'=>$pre_page,
				'next_page'=>$next_page,
				'page_count'=>$page_count
		);
	
		$params = array(
				'links' => $links ,
				'sp_config' => $sp_config
		);
	
		$this->load->view("admin/link/v_index",$params);
	}
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function edit($id = null)
	{
		$id = isset($_GET['id'])?$this->input->get("id"):null;
		if($id == null) // 添加
		{
			$link = $this->build_class('link');
			if(isset($_GET['class_id']))
			{
				$link->class_id = $this->input->get('class_id');
			}
		}
		else
		{
			$link = $this->link_manager->get($id);
		}
		
		if($this->is_postback())
		{
			$this->load->library('form_validation');
		
			if ($this->form_validation->run('link') == TRUE)
			{
				$link_post = $this->filter_post($_POST);
		
				if($id == null)  // 添加
				{
					$article['contribute_time'] = Date('Y-m-d');
					$id = $this->link_manager->insert($link_post);
				}
				else
				{
					$this->link_manager->update($link_post,array('id'=>$id));
					// redirect(SELF."/article/index");
				}
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl')."&key_id=$id")) ;
					exit;
				}
				$this->goto_last_index_page();
			}
		}
		
		$this->load->view("admin/link/v_form",$link);
		/*************************************************************
		$id = isset($_GET['id'])?$this->input->get('id'):$id;
		
		if(!$this->is_postback())
		{
			$link = new link_model();
			if(isset($_GET['class_id']))
			{
				$link->class_id = $this->input->get('class_id');
			}
			if($id != null) // 添加
			{
				$link = $this->link_manager->get($id);
			}
			$this->load->view("admin/link/v_form",$link); 
		}
		else
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('link') == TRUE)
			{
				$post_link = $this->filter_post();
				//print_r($_POST) ; 
				//print_r($post_link);exit;
				if($id == null)
				{
					$id = $this->link_manager->insert($post_link);
				}
				else
				{
					$this->link_manager->update($post_link,$id) ; 
				}
				
				$this->goto_last_index_page(); 
			}
			
			$this->load->view("admin/link/v_form",array('id'=>$id));  
		}
		******************************************************************/
	}
	
	/******************************************************
	* Profile		: 删除
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		// $this->link_manager->delete($id);
		// $this->goto_last_index_page() ;
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id))
		{
			$this->link_manager->delete($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				$this->link_manager->delete($id) ;
			}
		}
		$this->goto_last_index_page();
	}
	/******************************************************
	 * Profile		: 移动到
	* Author		: 宁秀娟
	* Create Time		: 2012-06-08
	* Modify Time		: 2012-06-08
	* Modify Profile	:
	******************************************************/
	public function moveto($class_id,$link_id=null)
	{
		$link_id = isset($_GET['id'])?$_GET['id']:$link_id;
		if(!empty($link_id))
		{
			$this->link_manager->moveto($class_id,$link_id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $link_id)
			{
				$this->link_manager->moveto($class_id,$link_id) ;
			}
		}
		$this->goto_last_index_page();
	}
} 